package offer;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Phone implements Runnable {
    public synchronized void sendMsg() {
        System.out.println(Thread.currentThread().getId() + "\t send msg");
        sendEmail();
    }

    public synchronized void sendEmail() {
        System.out.println(Thread.currentThread().getId() + "\t send email");
    }

    Lock lock = new ReentrantLock();

    public void run() {
        get();
    }

    public void get() {
        lock.lock();
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getId() + "\t get method");
            set();
        } finally {
            lock.unlock();
        }
    }

    public void set() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getId() + "\t set method");
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        new Thread(phone::sendMsg, "thread1").start();
        new Thread(phone::sendMsg, "thread2").start();

        Thread.sleep(3000);

        System.out.println();
        System.out.println();

        Thread thread3 = new Thread(phone);
        Thread thread4 = new Thread(phone);

        thread3.start();
        thread4.start();
    }
}
